package com.gdou.controller;


import com.gdou.commons.Result;
import com.gdou.dto.DeptDto;
import com.gdou.model.Dept;
import com.gdou.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("/getAllDepts")
    public Result getAllDepts(){
        return deptService.findAllDepts();
    }


    @RequestMapping("/getDeptsByPage")
    public Result getDeptsByPage(
            @RequestParam(required = false,defaultValue = "10") Integer pageSize,
            @RequestParam(required = false,defaultValue = "1") Integer pageNum
    ){
        return deptService.findDeptsByPage(pageNum,pageSize);
    }


    @RequestMapping("/addDepts")
    public Result addDepts(@Validated DeptDto deptDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.DATA_FORMAT_ERROR;
        }

        return deptService.saveDepts(deptDto);
    }

    /*删除部门*/
    @RequestMapping("/deleteDept")
    public Result deleteDept(Long id){
        if(id==null){
            return Result.DATA_FORMAT_ERROR;

        }
        return deptService.deleteDept(id);

    }
    /*修改部门*/
    @RequestMapping("/editDept")
    public Result editDept(Dept dept){

        return deptService.editDept(dept);
    }







    }
